﻿using System;
using Newtonsoft.Json.Converters;

namespace WinLiveHelper.Wp7
{
	public class IsoDateTimeToUtcConverter : IsoDateTimeConverter
	{
		public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
		{
			DateTime date;
			if (!DateTime.TryParse(value.ToString(), out date))
			{
				throw new Exception();
			}

			//NOTE: WinLive requires the DateTime specify a timezone. This is not done by default.
			//	By doing this we always convert the passed in date to Utc time with the default timezone.
			writer.WriteValue(date.ToUniversalTime().ToString("u"));
		}
	}
}
